#include "bluetoothwideget.h"
#include "ui_bluetoothwideget.h"
#include <QThread>
#include <QPainter>
#include <QRadialGradient>
#include <QImage>
#include <QMouseEvent>
#include <QDebug>
#include <QStatusBar>
#include "./plugIn/mydialog.h"


MyDialog MyDialog;

BluetoothWideget::BluetoothWideget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::BluetoothWideget)
{
    ui->setupUi(this);
    this->setWindowFlags(Qt::Window|Qt::FramelessWindowHint);

    this->setFixedSize(QApplication::desktop()->width(),QApplication::desktop()->height());

    ui->treeWidget->setContextMenuPolicy(Qt::CustomContextMenu); //使得该控件可以支持右键菜单
    connect(ui->treeWidget, SIGNAL(customContextMenuRequested(const QPoint &)),
            this, SLOT(SlotCustomContextMenuRequested(const QPoint&)));  //实现右击添加菜单的信号与槽的连接


        initPortWidget();
        initMenu();
        initLamp();
        initAllPressurePicture();
        initHeatMapPicture();

        portWideget->exec();
        detectionTime= new double;
        MyDialog.createdThreeFuctionDialog(this,detectionTime);
        ui->progressSlider->setRange(0, *detectionTime);
        portWideget->setDetectionTime(*detectionTime);


        connect(ui->progressBeginButton,SIGNAL(clicked()),this,SLOT(timeBegin()));
        connect(this,&BluetoothWideget::emitBeginSignal,portWideget,&serialPortWideget::threadSetting);
        connect(this,&BluetoothWideget::emitBeginSignalLeft,portWideget,&serialPortWideget::leftThreadSetting);

    //        connect(ui->progressSlider,&QSlider::sliderMoved,this,&BluetoothWideget::onSliderMoved);
    //        connect(ui->progressSlider,&QSlider::sliderReleased,this,&BluetoothWideget::onSliderReleased);
    //        connect(ui->progressSlider,SIGNAL(costomSliderClicked()),this,SLOT(onSliderClicked()));

    this->show();
}
BluetoothWideget::~BluetoothWideget()
{
   delete stepLed01;
   delete portWideget;
   delete status;
   delete menuBar;
   delete menu[10];

   delete double_foot;
   delete left_foot;
   delete right_foot;

   delete tracer;
   delete tracerLabel;


    for(int i = 0 ; i <plotList.size(); i++){
       delete plotList.at(i);
    }

    delete ui;

}
void BluetoothWideget::initMenu(){
//    status = new QStatusBar(ui->menuWidget);

        menu[0] = new QMenu("文件",ui->menuWidget);
//        menu[0]->addAction("打开串口");
        menu[0]->addAction("自定义设置检测时间");
//        menu[0]->addAction("四元数示例");
        menu[0]->addAction("文件上传");
        menu[0]->addAction("用户信息录入");
        menu[0]->addAction("modBusTcp通信");

//        act[0] = new QAction("新建",this);
//        act[0]->setShortcut(Qt::CTRL | Qt::Key_A );
//        act[0]->setStatusTip("这是一个新建菜单");

//        act[1] = new QAction("打开",this);
//        act[1]->setCheckable(true);

//        menu[1] = new QMenu("保存");
//        menu[1]->addAction(act[0]);


//        menu[2] = new QMenu("打印");
//        menu[2]->addAction("打印设置");
//        menu[2]->addMenu(menu[1]);

        menuBar = new QMenuBar(ui->menuWidget);
        for(int i = 0 ; i < 1;i++)
 {
            menuBar->addMenu(menu[i]);
}
        menuBar->setGeometry(0,0,this->width(),30);
        connect(menuBar,SIGNAL(triggered(QAction*)),this,SLOT(trigerMenu(QAction*)));


}
void BluetoothWideget::trigerMenu(QAction* act)
{
   if(act->text() == "自定义设置检测时间")
    {
        MyDialog.createdTimeDialog(this,ui->progressSlider);
    }else if(act->text() == "打开串口"){
       portWideget->show();
   }else if(act->text() == "四元数示例"){
      MyDialog.createdQuaternionDialog(this);
   }else if(act->text() == "文件上传"){
       MyDialog.createdUploadFile(this);
   }else if(act->text() == "用户信息录入"){
       MyDialog.createdUserInfo(this);
   }else if(act->text() == "modBusTcp通信"){
       MyDialog.createdModBusTcpDialog(this);
   }
}
void BluetoothWideget::initLamp(){
    stepLed01= MyDialog.createdStatusLamp(ui->ledWidget);
    leftLed= MyDialog.createdStatusLamp(ui->leftWidget);
    rightLed= MyDialog.createdStatusLamp(ui->rightWidget);


}
void BluetoothWideget::initAllPressurePicture()
{
    QWidget *leftFootTab =new QWidget();
    left_foot = new FOOT;
    left_foot->frontBackPressure.plot = new QCustomPlot;
    left_foot->frontMidBackPressure.plot = new QCustomPlot;
    left_foot->insideOutsidePressure.plot = new QCustomPlot;
    left_foot->bodyPressure.plot = new QCustomPlot;
    left_foot->allPressure.plot = new QCustomPlot;
    plotListL<<left_foot->allPressure.plot<<left_foot->frontBackPressure.plot<<left_foot->frontMidBackPressure.plot
                <<left_foot->insideOutsidePressure.plot<<left_foot->bodyPressure.plot;
    ui->tabWidget->addTab(leftFootTab,"leftFoot");
    MyDialog.initCustomPlot(this,leftFootTab,ui->treeWidget,plotListL);


    QWidget *rightFootTab =new QWidget();
    right_foot = new FOOT;
    right_foot->frontBackPressure.plot = new QCustomPlot;
    right_foot->frontMidBackPressure.plot = new QCustomPlot;
    right_foot->insideOutsidePressure.plot = new QCustomPlot;
    right_foot->bodyPressure.plot = new QCustomPlot;
    right_foot->allPressure.plot = new QCustomPlot;
    plotListR<<right_foot->allPressure.plot<<right_foot->frontBackPressure.plot<<right_foot->frontMidBackPressure.plot
                <<right_foot->insideOutsidePressure.plot<<right_foot->bodyPressure.plot;
    ui->tabWidget->addTab(rightFootTab,"rightFoot");
    MyDialog.initCustomPlot(this,rightFootTab,ui->treeWidget,plotListR);

    QWidget *FootTab =new QWidget();
    double_foot = new FOOT;
    double_foot->frontBackPressure.plot = new QCustomPlot;
    double_foot->frontMidBackPressure.plot = new QCustomPlot;
    double_foot->insideOutsidePressure.plot = new QCustomPlot;
    double_foot->bodyPressure.plot = new QCustomPlot;
    double_foot->allPressure.plot = new QCustomPlot;
    plotListAll<<double_foot->allPressure.plot<<double_foot->frontBackPressure.plot<<double_foot->frontMidBackPressure.plot
                <<double_foot->insideOutsidePressure.plot<<double_foot->bodyPressure.plot;
    ui->tabWidget->addTab(FootTab,"Foot");
    MyDialog.initCustomPlot(this,FootTab,ui->treeWidget,plotListAll);


    plotList<<plotListAll<<plotListR<<plotListL;

    connect(double_foot->allPressure.plot,&QCustomPlot::beforeReplot,this,[=](){
        double_foot->allPressure.graph.pGraph1_1 = double_foot->allPressure.plot->addGraph();
        double_foot->allPressure.graph.pGraph1_2 = double_foot->allPressure.plot->addGraph();
        double_foot->allPressure.graph.pGraph1_3 = double_foot->allPressure.plot->addGraph();
        double_foot->allPressure.graph.pGraph1_4 = double_foot->allPressure.plot->addGraph();
        double_foot->allPressure.graph.pGraph1_5 = double_foot->allPressure.plot->addGraph();
        double_foot->allPressure.graph.pGraph1_6 = double_foot->allPressure.plot->addGraph();
        double_foot->allPressure.graph.pGraph1_7 = double_foot->allPressure.plot->addGraph();
        double_foot->allPressure.graph.pGraph1_8 = double_foot->allPressure.plot->addGraph();
        double_foot->allPressure.graph.pGraph1_9 = double_foot->allPressure.plot->addGraph();

        double_foot->allPressure.graph.pGraph1_1->setPen(QPen(Qt::red));
        double_foot->allPressure.graph.pGraph1_2->setPen(QPen(Qt::white));
        double_foot->allPressure.graph.pGraph1_3->setPen(QPen(Qt::green));
        double_foot->allPressure.graph.pGraph1_4->setPen(QPen(Qt::yellow));
        double_foot->allPressure.graph.pGraph1_5->setPen(QPen(Qt::gray));
        double_foot->allPressure.graph.pGraph1_6->setPen(QPen(Qt::darkGray));
        double_foot->allPressure.graph.pGraph1_7->setPen(QPen(Qt::cyan));
        double_foot->allPressure.graph.pGraph1_8->setPen(QPen(Qt::darkBlue));
        double_foot->allPressure.graph.pGraph1_9->setPen(QPen(Qt::darkYellow));
        initPlot(double_foot->allPressure.plot,"双脚——九区域足底压力曲线图");
    });
    connect(double_foot->frontMidBackPressure.plot,&QCustomPlot::beforeReplot,this,[=](){
        double_foot->frontMidBackPressure.graph.FMBpGraph1_1 = double_foot->frontMidBackPressure.plot->addGraph();
        double_foot->frontMidBackPressure.graph.FMBpGraph1_2 = double_foot->frontMidBackPressure.plot->addGraph();
        double_foot->frontMidBackPressure.graph.FMBpGraph1_3 = double_foot->frontMidBackPressure.plot->addGraph();

        double_foot->frontMidBackPressure.graph.FMBpGraph1_1->setPen(QPen(Qt::red));
        double_foot->frontMidBackPressure.graph.FMBpGraph1_2->setPen(QPen(Qt::white));
        double_foot->frontMidBackPressure.graph.FMBpGraph1_3->setPen(QPen(Qt::green));

        initPlot(double_foot->frontMidBackPressure.plot,"双脚——前中后足底压力曲线图");
    });
    connect(double_foot->frontBackPressure.plot,&QCustomPlot::beforeReplot,this,[=](){
        double_foot->frontBackPressure.graph.FBGraph1_1 = double_foot->frontBackPressure.plot->addGraph();
        double_foot->frontBackPressure.graph.FBGraph1_2 = double_foot->frontBackPressure.plot->addGraph();

        double_foot->frontBackPressure.graph.FBGraph1_1->setPen(QPen(Qt::red));
        double_foot->frontBackPressure.graph.FBGraph1_2->setPen(QPen(Qt::white));

        initPlot(double_foot->frontBackPressure.plot,"双脚——前后足底压力曲线图");
    });
    connect(double_foot->insideOutsidePressure.plot,&QCustomPlot::beforeReplot,this,[=](){
       double_foot->insideOutsidePressure.graph.IOGraph1_1 = double_foot->insideOutsidePressure.plot->addGraph();
       double_foot->insideOutsidePressure.graph.IOGraph1_2 = double_foot->insideOutsidePressure.plot->addGraph();

        double_foot->insideOutsidePressure.graph.IOGraph1_1->setPen(QPen(Qt::red));
        double_foot->insideOutsidePressure.graph.IOGraph1_2->setPen(QPen(Qt::white));

        initPlot(double_foot->insideOutsidePressure.plot,"双脚——内外侧足底压力曲线图");
    });
    connect(double_foot->bodyPressure.plot,&QCustomPlot::beforeReplot,this,[=](){
        double_foot->bodyPressure.graph.BGraph1_1 = double_foot->bodyPressure.plot->addGraph();
        double_foot->bodyPressure.graph.BGraph1_2 = double_foot->bodyPressure.plot->addGraph();

        double_foot->bodyPressure.graph.BGraph1_1->setPen(QPen(Qt::red));
        double_foot->bodyPressure.graph.BGraph1_2->setPen(QPen(Qt::white));

        initPlot(double_foot->bodyPressure.plot,"双脚——身体平衡压力曲线图");

    });

    connect(left_foot->allPressure.plot,&QCustomPlot::beforeReplot,this,[=](){
        left_foot->allPressure.graph.pGraph1_1 = left_foot->allPressure.plot->addGraph();
        left_foot->allPressure.graph.pGraph1_2 = left_foot->allPressure.plot->addGraph();
        left_foot->allPressure.graph.pGraph1_3 = left_foot->allPressure.plot->addGraph();
        left_foot->allPressure.graph.pGraph1_4 = left_foot->allPressure.plot->addGraph();
        left_foot->allPressure.graph.pGraph1_5 = left_foot->allPressure.plot->addGraph();
        left_foot->allPressure.graph.pGraph1_6 = left_foot->allPressure.plot->addGraph();
        left_foot->allPressure.graph.pGraph1_7 = left_foot->allPressure.plot->addGraph();
        left_foot->allPressure.graph.pGraph1_8 = left_foot->allPressure.plot->addGraph();
        left_foot->allPressure.graph.pGraph1_9 = left_foot->allPressure.plot->addGraph();

        left_foot->allPressure.graph.pGraph1_1->setPen(QPen(Qt::red));
        left_foot->allPressure.graph.pGraph1_2->setPen(QPen(Qt::white));
        left_foot->allPressure.graph.pGraph1_3->setPen(QPen(Qt::green));
        left_foot->allPressure.graph.pGraph1_4->setPen(QPen(Qt::yellow));
        left_foot->allPressure.graph.pGraph1_5->setPen(QPen(Qt::gray));
        left_foot->allPressure.graph.pGraph1_6->setPen(QPen(Qt::darkGray));
        left_foot->allPressure.graph.pGraph1_7->setPen(QPen(Qt::cyan));
        left_foot->allPressure.graph.pGraph1_8->setPen(QPen(Qt::darkBlue));
        left_foot->allPressure.graph.pGraph1_9->setPen(QPen(Qt::darkYellow));
        initPlot(left_foot->allPressure.plot,"左脚——九区域足底压力曲线图");
    });
    connect(left_foot->frontMidBackPressure.plot,&QCustomPlot::beforeReplot,this,[=](){
        left_foot->frontMidBackPressure.graph.FMBpGraph1_1 = left_foot->frontMidBackPressure.plot->addGraph();
        left_foot->frontMidBackPressure.graph.FMBpGraph1_2 = left_foot->frontMidBackPressure.plot->addGraph();
        left_foot->frontMidBackPressure.graph.FMBpGraph1_3 = left_foot->frontMidBackPressure.plot->addGraph();

        left_foot->frontMidBackPressure.graph.FMBpGraph1_1->setPen(QPen(Qt::red));
        left_foot->frontMidBackPressure.graph.FMBpGraph1_2->setPen(QPen(Qt::white));
        left_foot->frontMidBackPressure.graph.FMBpGraph1_3->setPen(QPen(Qt::green));

        initPlot(left_foot->frontMidBackPressure.plot,"左脚——前中后足底压力曲线图");
    });
    connect(left_foot->frontBackPressure.plot,&QCustomPlot::beforeReplot,this,[=](){
        left_foot->frontBackPressure.graph.FBGraph1_1 = left_foot->frontBackPressure.plot->addGraph();
        left_foot->frontBackPressure.graph.FBGraph1_2 = left_foot->frontBackPressure.plot->addGraph();

        left_foot->frontBackPressure.graph.FBGraph1_1->setPen(QPen(Qt::red));
        left_foot->frontBackPressure.graph.FBGraph1_2->setPen(QPen(Qt::white));

        initPlot(left_foot->frontBackPressure.plot,"左脚——前后足底压力曲线图");
    });
    connect(left_foot->insideOutsidePressure.plot,&QCustomPlot::beforeReplot,this,[=](){
       left_foot->insideOutsidePressure.graph.IOGraph1_1 = left_foot->insideOutsidePressure.plot->addGraph();
       left_foot->insideOutsidePressure.graph.IOGraph1_2 = left_foot->insideOutsidePressure.plot->addGraph();

        left_foot->insideOutsidePressure.graph.IOGraph1_1->setPen(QPen(Qt::red));
        left_foot->insideOutsidePressure.graph.IOGraph1_2->setPen(QPen(Qt::white));

        initPlot(left_foot->insideOutsidePressure.plot,"左脚——内外侧足底压力曲线图");
    });
    connect(left_foot->bodyPressure.plot,&QCustomPlot::beforeReplot,this,[=](){
        left_foot->bodyPressure.graph.BGraph1_1 = left_foot->bodyPressure.plot->addGraph();
        left_foot->bodyPressure.graph.BGraph1_2 = left_foot->bodyPressure.plot->addGraph();

        left_foot->bodyPressure.graph.BGraph1_1->setPen(QPen(Qt::red));
        left_foot->bodyPressure.graph.BGraph1_2->setPen(QPen(Qt::white));

        initPlot(left_foot->bodyPressure.plot,"左脚——身体平衡压力曲线图");

    });


    connect(right_foot->allPressure.plot,&QCustomPlot::beforeReplot,this,[=](){
        right_foot->allPressure.graph.pGraph1_1 = right_foot->allPressure.plot->addGraph();
        right_foot->allPressure.graph.pGraph1_2 = right_foot->allPressure.plot->addGraph();
        right_foot->allPressure.graph.pGraph1_3 = right_foot->allPressure.plot->addGraph();
        right_foot->allPressure.graph.pGraph1_4 = right_foot->allPressure.plot->addGraph();
        right_foot->allPressure.graph.pGraph1_5 = right_foot->allPressure.plot->addGraph();
        right_foot->allPressure.graph.pGraph1_6 = right_foot->allPressure.plot->addGraph();
        right_foot->allPressure.graph.pGraph1_7 = right_foot->allPressure.plot->addGraph();
        right_foot->allPressure.graph.pGraph1_8 = right_foot->allPressure.plot->addGraph();
        right_foot->allPressure.graph.pGraph1_9 = right_foot->allPressure.plot->addGraph();

        right_foot->allPressure.graph.pGraph1_1->setPen(QPen(Qt::red));
        right_foot->allPressure.graph.pGraph1_2->setPen(QPen(Qt::white));
        right_foot->allPressure.graph.pGraph1_3->setPen(QPen(Qt::green));
        right_foot->allPressure.graph.pGraph1_4->setPen(QPen(Qt::yellow));
        right_foot->allPressure.graph.pGraph1_5->setPen(QPen(Qt::gray));
        right_foot->allPressure.graph.pGraph1_6->setPen(QPen(Qt::darkGray));
        right_foot->allPressure.graph.pGraph1_7->setPen(QPen(Qt::cyan));
        right_foot->allPressure.graph.pGraph1_8->setPen(QPen(Qt::darkBlue));
        right_foot->allPressure.graph.pGraph1_9->setPen(QPen(Qt::darkYellow));
        initPlot(right_foot->allPressure.plot,"右脚——九区域足底压力曲线图");
    });
    connect(right_foot->frontMidBackPressure.plot,&QCustomPlot::beforeReplot,this,[=](){
        right_foot->frontMidBackPressure.graph.FMBpGraph1_1 = right_foot->frontMidBackPressure.plot->addGraph();
        right_foot->frontMidBackPressure.graph.FMBpGraph1_2 = right_foot->frontMidBackPressure.plot->addGraph();
        right_foot->frontMidBackPressure.graph.FMBpGraph1_3 = right_foot->frontMidBackPressure.plot->addGraph();

        right_foot->frontMidBackPressure.graph.FMBpGraph1_1->setPen(QPen(Qt::red));
        right_foot->frontMidBackPressure.graph.FMBpGraph1_2->setPen(QPen(Qt::white));
        right_foot->frontMidBackPressure.graph.FMBpGraph1_3->setPen(QPen(Qt::green));

        initPlot(right_foot->frontMidBackPressure.plot,"右脚——前中后足底压力曲线图");
    });
    connect(right_foot->frontBackPressure.plot,&QCustomPlot::beforeReplot,this,[=](){
        right_foot->frontBackPressure.graph.FBGraph1_1 = right_foot->frontBackPressure.plot->addGraph();
        right_foot->frontBackPressure.graph.FBGraph1_2 = right_foot->frontBackPressure.plot->addGraph();

        right_foot->frontBackPressure.graph.FBGraph1_1->setPen(QPen(Qt::red));
        right_foot->frontBackPressure.graph.FBGraph1_2->setPen(QPen(Qt::white));

        initPlot(right_foot->frontBackPressure.plot,"右脚——前后足底压力曲线图");
    });
    connect(right_foot->insideOutsidePressure.plot,&QCustomPlot::beforeReplot,this,[=](){
       right_foot->insideOutsidePressure.graph.IOGraph1_1 = right_foot->insideOutsidePressure.plot->addGraph();
       right_foot->insideOutsidePressure.graph.IOGraph1_2 = right_foot->insideOutsidePressure.plot->addGraph();

        right_foot->insideOutsidePressure.graph.IOGraph1_1->setPen(QPen(Qt::red));
        right_foot->insideOutsidePressure.graph.IOGraph1_2->setPen(QPen(Qt::white));

        initPlot(right_foot->insideOutsidePressure.plot,"右脚——内外侧足底压力曲线图");
    });
    connect(right_foot->bodyPressure.plot,&QCustomPlot::beforeReplot,this,[=](){
        right_foot->bodyPressure.graph.BGraph1_1 = right_foot->bodyPressure.plot->addGraph();
        right_foot->bodyPressure.graph.BGraph1_2 = right_foot->bodyPressure.plot->addGraph();

        right_foot->bodyPressure.graph.BGraph1_1->setPen(QPen(Qt::red));
        right_foot->bodyPressure.graph.BGraph1_2->setPen(QPen(Qt::white));

        initPlot(right_foot->bodyPressure.plot,"右脚——身体平衡压力曲线图");

    });


}
void BluetoothWideget::initPortWidget(){
    portWideget = new serialPortWideget();
    connect(portWideget,&serialPortWideget::emitPortInfo,this,[=](){
        rightLed->setStates(QSimpleLed::ON);
    });
    connect(portWideget,&serialPortWideget::emitPortLeftInfo,this,[=](){
        leftLed->setStates(QSimpleLed::ON);
    });
    connect(portWideget,&serialPortWideget::emitCloseRight,this,[=](){
        rightLed->setStates(QSimpleLed::OFF);
    });
    connect(portWideget,&serialPortWideget::emitCloseLeft,this,[=](){
        leftLed->setStates(QSimpleLed::OFF);
    });
}


void BluetoothWideget::timeBegin(){

    if(modesNumber<modeCheck.size()){
          MyDialog.createCurrentModeShow(this,modeCheck[modesNumber]);
          modesNumber=modesNumber+1;
      }
    timer = new QTimer(this);
    timer->start(10);
    emit emitBeginSignal();
    emit emitBeginSignalLeft();

    connect(timer,SIGNAL(timeout()),this,SLOT(TimeDataUpdate()));
    if(timer->isActive()){
                stepLed01->setStates(QSimpleLed::ON);
           }
    ui->progressBeginButton->setEnabled(false);
}
// 定时器溢出处理槽函数。用来生成曲线的坐标数据。
void BluetoothWideget::TimeDataUpdate()
{
//  生成坐标数据
    updateData();

    static QTime time(QTime::currentTime());
    if(time.elapsed()/1000.0>current_time_stop){
        for(int j = 0 ; j <plotList.size();j++)
        {
            for(int i=0;i<plotList.at(j)->graphCount();++i)
               {
                  plotList.at(j)->graph(i)->data().data()->clear();
               }
        }
        time.restart();
        current_time_stop = 9999999999;
    }
    timeX = time.elapsed()/1000.0; // 开始到现在的时间，单位秒 //timeX 为自变量x
    ui->progressSlider->setValue(timeX);
    ui->progressTimeShow->setText(QString::asprintf("%2.2f",timeX)+" s");
//    计算帧数
    static double lastFpsKey;
    static int frameCount;
    ++frameCount;
    if (timeX-lastFpsKey > 1) // 每1秒求一次平均值
    {
        ui->FPSlabel->setText(QString("%1 FPS").arg(frameCount/(timeX-lastFpsKey), 0, 'f', 0));//显示帧数
        lastFpsKey = timeX;
        frameCount = 0;
    }
//    判断是否结束
    if(timeX > *detectionTime){
        timeStop();
    }
}

void BluetoothWideget::timeStop(){
    current_time_stop  = timeX;
    timer->stop();
    delete timer;

    ui->progressTimeShow->setText(QString::asprintf("%2.2f",*detectionTime)+" s");
    ui->progressSlider->setValue(*detectionTime);
    stepLed01->setStates(QSimpleLed::OFF);

//    qDebug()<<"stop time："<<QString::asprintf("%2.2f",timeX)<<"s";
    ui->progressBeginButton->setEnabled(true);

    if(!modeCheck.isEmpty() && modesNumber>=modeCheck.size()){
           modesNumber = 0;
           modeCheck.clear();
           double *time = new double;
           MyDialog.createFinishTest(this,time,detectionTime);
           ui->progressTimeShow->setText(QString::asprintf("%2.2f",*time)+" s");
           ui->progressSlider->setValue(*time);

           ui->progressSlider->setRange(0, *detectionTime);
           portWideget->setDetectionTime(*detectionTime);
           delete time;

        }
}



//初始化热力图数据
void BluetoothWideget::initHeatMapPicture()
{
    ui->widget_leftFoot->installEventFilter(this);
    ui->widget_rightFoot->installEventFilter(this);
    //左脚图片
    ui->widget_leftFoot->setAutoFillBackground(true);  // 这句要加上, 否则可能显示不出背景图.

    QPalette palette = ui->widget_leftFoot->palette();
    palette.setBrush(QPalette::Background,
    QBrush(QPixmap(":/Picture/foot1.png").scaled( // 缩放背景图.
    ui->widget_leftFoot->size(),
    Qt::IgnoreAspectRatio,
    Qt::SmoothTransformation)));  // 使用平滑的缩放方式
    ui->widget_leftFoot->setPalette(palette);  // 至此, 已给widget加上了背景图.

    //左脚图片_覆盖
    ui->widget_leftFoot_up->setAutoFillBackground(true);  // 这句要加上, 否则可能显示不出背景图.
    QPalette palette_up = ui->widget_leftFoot_up->palette();
    palette_up.setBrush(QPalette::Background,
    QBrush(QPixmap(":/Picture/foot1.png").scaled( // 缩放背景图.
    ui->widget_leftFoot_up->size(),
    Qt::IgnoreAspectRatio,
    Qt::SmoothTransformation)));  // 使用平滑的缩放方式
    ui->widget_leftFoot_up->setPalette(palette_up);  // 至此, 已给widget加上了背景图.

    //右脚图片
    ui->widget_rightFoot->setAutoFillBackground(true);  // 这句要加上, 否则可能显示不出背景图.
    QPalette palette1 = ui->widget_rightFoot->palette();
    palette1.setBrush(QPalette::Background,
    QBrush(QPixmap(":/Picture/foot2.png").scaled( // 缩放背景图.
    ui->widget_rightFoot->size(),
    Qt::IgnoreAspectRatio,
    Qt::SmoothTransformation)));  // 使用平滑的缩放方式
    ui->widget_rightFoot->setPalette(palette1);  // 至此, 已给widget加上了背景图.

    //右脚图片_覆盖
    ui->widget_rightFoot_up->setAutoFillBackground(true);  // 这句要加上, 否则可能显示不出背景图.
    QPalette palette1_up = ui->widget_rightFoot_up->palette();
    palette1_up.setBrush(QPalette::Background,
    QBrush(QPixmap(":/Picture/foot2.png").scaled( // 缩放背景图.
    ui->widget_rightFoot_up->size(),
    Qt::IgnoreAspectRatio,
    Qt::SmoothTransformation)));  // 使用平滑的缩放方式
    ui->widget_rightFoot_up->setPalette(palette1_up);  // 至此, 已给widget加上了背景图.


    palette_ = new GradientPalette(DEFAULT_WIDTH);

    palette_->setColorAt(0.45, Qt::blue);
    palette_->setColorAt(0.55, Qt::cyan);
    palette_->setColorAt(0.65, Qt::green);
    palette_->setColorAt(0.85, Qt::yellow);
    palette_->setColorAt(1.0, Qt::red);

    canvas_right = new QImage(this->width(), this->height(), QImage::Format_ARGB32);
    canvas_right->fill(QColor(0, 0, 0, 0));

    mapper_right = new HeatMapper(canvas_right, palette_, DEFAULT_RADIUS, DEFAULT_OPACITY);

    canvas_left = new QImage(this->width(), this->height(), QImage::Format_ARGB32);
    canvas_left->fill(QColor(0, 0, 0, 0));

    mapper_left = new HeatMapper(canvas_left, palette_, DEFAULT_RADIUS, DEFAULT_OPACITY);
    this->setFixedSize(this->width(), this->height());

}
//事件过滤器
bool BluetoothWideget::eventFilter(QObject *watched, QEvent *event)
{
    if(watched == ui->widget_leftFoot  && event->type() == QEvent::Paint)
    {
        QPainter painter_left(ui->widget_leftFoot);
        painter_left.drawImage(0, 0, *canvas_left);

    }
    if(watched == ui->widget_rightFoot  && event->type() == QEvent::Paint)
    {
        QPainter painter_right(ui->widget_rightFoot);
        painter_right.drawImage(0, 0, *canvas_right);

    }


    return QWidget::eventFilter(watched,event);
}
//热力图传值
void BluetoothWideget::showHeatMap(QList<int> pressure, QList<int> sortedIndex ,QList<int> pressureTemp,int type){
    switch(type){
    case 0 :
        canvas_right->fill(Qt::transparent);
        mapper_right->clearData();
              //先画最大值坐标
        switch (sortedIndex[0]) {
                    case 8:  //A9坐标
                     mapper_right->addPoint(140,420,pressureTemp[0]*50);
                    break;
                    case 7:  //A8坐标
                     mapper_right->addPoint(100,380,pressureTemp[0]*50);
                 break;
                    case 6:  //A7坐标
                     mapper_right->addPoint(170,360,pressureTemp[0]*50);
                 break;
                    case 5:  //A6坐标
                     mapper_right->addPoint(180,290,pressureTemp[0]*50);
                 break;
                    case 4:  //A5坐标
                     mapper_right->addPoint(100,280,pressureTemp[0]*50);
                 break;
                    case 3:  //A4坐标
                     mapper_right->addPoint(190,210,pressureTemp[0]*50);
                 break;
                   case 2:   //A3坐标
                     mapper_right->addPoint(80,190,pressureTemp[0]*50);
                 break;
                   case 1:   //A2坐标
                     mapper_right->addPoint(150,160,pressureTemp[0]*50);
                 break;
                   case 0:   //A1坐标
                     mapper_right->addPoint(100,130,pressureTemp[0]*50);
                 break;
                }
          //再画剩下坐标
        for(int i = 1; i<pressure.length();i++) {
                    switch (sortedIndex[i]) {
                        case 8:  //A9坐标
                         mapper_right->addPoint(140,420,pressure[i]*50);
                                           mapper_right->addPoint(140,420,pressure[i]*50);
                        break;
                        case 7:  //A8坐标
                         mapper_right->addPoint(100,380,pressure[i]*50);
                                          mapper_right->addPoint(100,380,pressure[i]*50);
                     break;
                        case 6:  //A7坐标
                         mapper_right->addPoint(170,360,pressure[i]*50);
                                               mapper_right->addPoint(170,360,pressure[i]*50);
                     break;
                        case 5:  //A6坐标
                         mapper_right->addPoint(180,290,pressure[i]*50);
                                           mapper_right->addPoint(180,290,pressure[i]*50);
                     break;
                        case 4:  //A5坐标
                         mapper_right->addPoint(100,280,pressure[i]*50);
                                            mapper_right->addPoint(100,280,pressure[i]*50);
                     break;
                        case 3:  //A4坐标
                         mapper_right->addPoint(190,210,pressure[i]*50);
                                            mapper_right->addPoint(190,210,pressure[i]*50);
                     break;
                       case 2:   //A3坐标
                         mapper_right->addPoint(80,190,pressure[i]*50);
                                            mapper_right->addPoint(80,190,pressure[i]*50);
                     break;
                       case 1:   //A2坐标
                         mapper_right->addPoint(150,160,pressure[i]*50);
                                            mapper_right->addPoint(150,160,pressure[i]*50);
                     break;
                       case 0:   //A1坐标
                         mapper_right->addPoint(100,130,pressure[i]*50);
                                           mapper_right->addPoint(100,130,pressure[i]*50);
                     break;
                    }
              }
        ui->widget_rightFoot->update();
        break;
    case 1 :
        canvas_left->fill(Qt::transparent);
        mapper_left->clearData();
              //先画最大值坐标
        switch (sortedIndex[0]) {
                        case 8:  //A9坐标
                         mapper_left->addPoint(140,420,pressureTemp[0]*50);
                        break;
                        case 7:  //A8坐标
                         mapper_left->addPoint(100,380,pressureTemp[0]*50);
                     break;
                        case 6:  //A7坐标
                         mapper_left->addPoint(170,360,pressureTemp[0]*50);
                     break;
                        case 5:  //A6坐标
                         mapper_left->addPoint(180,290,pressureTemp[0]*50);
                     break;
                        case 4:  //A5坐标
                         mapper_left->addPoint(100,280,pressureTemp[0]*50);
                     break;
                        case 3:  //A4坐标
                         mapper_left->addPoint(190,210,pressureTemp[0]*50);
                     break;
                       case 2:   //A3坐标
                         mapper_left->addPoint(80,190,pressureTemp[0]*50);
                     break;
                       case 1:   //A2坐标
                         mapper_left->addPoint(150,160,pressureTemp[0]*50);
                     break;
                       case 0:   //A1坐标
                         mapper_left->addPoint(100,130,pressureTemp[0]*50);
                     break;
                    }
              //再画剩下坐标
        for(int i = 1; i<pressure.length();i++) {
                    switch (sortedIndex[i]) {
                        case 8:  //A9坐标
                         mapper_left->addPoint(140,420,pressure[i]*50);
                                           mapper_left->addPoint(140,420,pressure[i]*50);
                        break;
                        case 7:  //A8坐标
                         mapper_left->addPoint(100,380,pressure[i]*50);
                                          mapper_left->addPoint(100,380,pressure[i]*50);
                     break;
                        case 6:  //A7坐标
                         mapper_left->addPoint(170,360,pressure[i]*50);
                                               mapper_left->addPoint(170,360,pressure[i]*50);
                     break;
                        case 5:  //A6坐标
                         mapper_left->addPoint(180,290,pressure[i]*50);
                                           mapper_left->addPoint(180,290,pressure[i]*50);
                     break;
                        case 4:  //A5坐标
                         mapper_left->addPoint(100,280,pressure[i]*50);
                                            mapper_left->addPoint(100,280,pressure[i]*50);
                     break;
                        case 3:  //A4坐标
                         mapper_left->addPoint(190,210,pressure[i]*50);
                                            mapper_left->addPoint(190,210,pressure[i]*50);
                     break;
                       case 2:   //A3坐标
                         mapper_left->addPoint(80,190,pressure[i]*50);
                                            mapper_left->addPoint(80,190,pressure[i]*50);
                     break;
                       case 1:   //A2坐标
                         mapper_left->addPoint(150,160,pressure[i]*50);
                                            mapper_left->addPoint(150,160,pressure[i]*50);
                     break;
                       case 0:   //A1坐标
                         mapper_left->addPoint(100,130,pressure[i]*50);
                                           mapper_left->addPoint(100,130,pressure[i]*50);
                     break;
                    }
              }
        ui->widget_leftFoot->update();
        break;
    }
        pressure.clear();
        sortedIndex.clear();
        pressureTemp.clear();


}






//void BluetoothWideget::onSliderMoved(int value)
//{
//    timer->stop();

//    ui->progressSlider->setValue(value);
//    ui->progressTimeShow->setText(QString::number(value)+":00 s");
//    timeX = value;

//    //对曲线进行处理，这是时间的值value
//}

//void BluetoothWideget::onSliderReleased()
//{
//    timer->start(15);
//}

//void BluetoothWideget::onSliderClicked()
//{
//    ui->progressSlider->setValue(ui->progressSlider->value());
//    ui->progressTimeShow->setText(QString::number(ui->progressSlider->value())+":00 s");
//    timeX = ui->progressSlider->value();
//    //对曲线进行处理，这是时间的值ui->progressSlider->value()
//}



//对plot统一处理初始化
void BluetoothWideget::initPlot(QCustomPlot *customPlot,QString plotName)
{
    customPlot->setStyleSheet(QString::fromUtf8("font:20px\"Time New Roman\";"));
    customPlot->xAxis->setLabel("时间");
    customPlot->yAxis->setLabel("压力值");


    for(int i=0;i<customPlot->graphCount();i++){// 添加曲线名称
         customPlot->graph(i)->setName("区域"+QString::number(i+1));
     }
    customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectPlottables | QCP::iSelectLegend);// 允许用户用鼠标拖动轴范围，用鼠标滚轮缩放，点击选择图形:
    connect(customPlot, &QCustomPlot::selectionChangedByUser, this,[=](){selectionChanged(customPlot);});        //legend与坐标曲线同步连接
    connect(customPlot, SIGNAL(mouseMove(QMouseEvent*)), this, SLOT(mouseMove(QMouseEvent*)));
    connect(customPlot, &QCustomPlot::mouseMove, this,[=]() mutable{
        if(timeX > *detectionTime){
        for (int i = 0; i < customPlot->graphCount(); ++i)
        {
            QCPPlottableLegendItem *item = customPlot->legend->itemWithPlottable(customPlot->graph(i));
            if (item->selected() ||customPlot->graph(i)->selected())//选中了哪条曲线或者曲线的图例
            {
                tracer = new QCPItemTracer(customPlot);
                tracer->setPen(QPen(Qt::red));//圆圈轮廓颜色
                tracer->setBrush(QBrush(Qt::red));//圆圈圈内颜色
                tracer->setStyle(QCPItemTracer::tsCircle);//圆圈
                tracer->setSize(5);//设置大小
                tracer->setGraph(customPlot->graph(i));

                tracerLabel = new QCPItemText(customPlot); //生成游标说明
                tracerLabel->setLayer("overlay");//设置图层为overlay，因为需要频繁刷新
                tracerLabel->setPen(QPen(QColor(255,255,255,200)));//设置游标说明颜色
                tracerLabel->setBrush(QBrush(QColor(255,255,255,150)));
                tracerLabel->setPositionAlignment( Qt::AlignTop);//左上
                tracerLabel->position->setParentAnchor(tracer->position);//将游标说明锚固在tracer位置处，实现自动跟随

               //获得鼠标位置处对应的横坐标数据x

                double valueX = customPlot->xAxis->pixelToCoord(x);
                QCPDataContainer<QCPGraphData>::const_iterator coorPoint = customPlot->graph(i)->data().data()->findBegin(valueX, true);
//                double valueY = customPlot->yAxis->pixelToCoord(coorPoint->value);
//                qDebug() << QString("graph%1对应的Y值是").arg(i) << coorPoint->value;

               tracer->setGraphKey(valueX); //将游标横坐标（key）设置成刚获得的横坐标数据x
               tracer->setInterpolating(true); //游标的纵坐标可以通过曲线数据线性插值自动获得
               tracer->updatePosition(); //使得刚设置游标的横纵坐标位置生效

//               qDebug()<<"x:"<<valueX<<" y:"<<valueY;
               tracer->position->setCoords(valueX, coorPoint->value);//设置游标位置

               tracerLabel->setText(QString("x = %1 s, y = %2 N").arg(valueX).arg(coorPoint->value));//设置游标说明内容
               tracerLabel->visible();

               customPlot->replot();//绘制器一定要重绘，否则看不到游标位置更新情况
               tracerLabel->setVisible(false);
               tracer->setVisible(false);

            }
        }
           }
    });

    customPlot->legend->setVisible(true);//图例显示
    customPlot->legend->setBrush(QColor(255,255,255,70));//图例背景颜色
//        allPressurePlot->axisRect()->insetLayout()->setInsetAlignment(0,Qt::AlignBottom|Qt::AlignRight);//图例位置
//        allPressurePlot->legend->setBorderPen(Qt::NoPen);//图例不画边框
    customPlot->legend->setWrap(9);//设置九个图例自动换行
    customPlot->legend->setFillOrder(QCPLayoutGrid::foColumnsFirst);//图例 行 优先排序
    customPlot->plotLayout()->addElement(1 , 0, customPlot->legend); //设置图例位置,坐标曲线的下方
    customPlot->plotLayout()->setRowStretchFactor(1, 0.001);//图例显示比例
    customPlot->plotLayout()->insertRow(0);// 插入一行
    QCPTextElement *allPressureTitle = new QCPTextElement(customPlot,plotName,QFont("sans",10,QFont::Bold));
    allPressureTitle->setTextColor(Qt::white);
    allPressureTitle->setMargins(QMargins(0,10,0,5));// 为了效果更好，添加一些边距
    customPlot->plotLayout()->addElement(0, 0, allPressureTitle);// 在第一行第一列添加标题

    QLinearGradient plotGradient;
    plotGradient.setStart(0,0);//背景颜色起始点，从图左上角开始，y方向0~400之间为渐变
    plotGradient.setFinalStop(0,400);
    plotGradient.setColorAt(0,QColor(0,51,102));//透明度从 0 ~ 1，
    plotGradient.setColorAt(0.85,QColor(103,171,214));
    plotGradient.setColorAt(1,QColor(103,171,214));
    customPlot->setBackground(plotGradient);

    customPlot->xAxis->setBasePen(QPen(Qt::white,2));// x轴线的画笔颜色和粗细
    customPlot->xAxis->setTickPen(QPen(Qt::white,3));// x轴线上的主刻度线（有数字的刻度线）的画笔颜色和粗细
    customPlot->xAxis->setTickLabelColor(Qt::white);// x轴线上的主刻度线下的文字颜色
    customPlot->xAxis->setTickLengthIn(6);// 轴线内主刻度线的长度
    customPlot->xAxis->setTickLengthOut(15);// 轴线外主刻度线的长度
    customPlot->xAxis->setSubTickPen(QPen(QColor(255,255,255),1));//粉红色，x轴线上的子刻度线（有数字的刻度线）的画笔颜色和粗细
    customPlot->xAxis->setLabelColor(Qt::white);// 只有设置了标签，轴标签的颜色才会显示
    customPlot->xAxis->setUpperEnding(QCPLineEnding::esSpikeArrow);// 设置轴线结束时的风格为 实角三角形但内部有凹陷的形状
    customPlot->xAxis->setLowerEnding(QCPLineEnding::esDisc);//setLowerEnding设置轴线开始时的风格

    customPlot->yAxis->setBasePen(QPen(Qt::white,2));// x轴线的画笔颜色和粗细
    customPlot->yAxis->setTickPen(QPen(Qt::white,3));// x轴线上的主刻度线（有数字的刻度线）的画笔颜色和粗细
    customPlot->yAxis->setTickLabelColor(Qt::white);// x轴线上的主刻度线下的文字颜色
    customPlot->yAxis->setLabelColor(Qt::white);// 只有设置了标签，轴标签的颜色才会显示

    //设置QCPGrid网格的风格,每条网格对应一个刻度
    customPlot->xAxis->grid()->setPen(QPen(QColor(255,255,255),2,Qt::DotLine));//虚线
    customPlot->xAxis->grid()->setSubGridVisible(true);// 显示x轴子网格线
    customPlot->xAxis->grid()->setZeroLinePen(QPen(Qt::black,3));// 设置刻度为0时的网格线的画笔
    customPlot->yAxis->grid()->setZeroLinePen(QPen(Qt::black,3));
    QLinearGradient axisRectGradient;
    axisRectGradient.setStart(0,0);
    axisRectGradient.setFinalStop(0,350);
    axisRectGradient.setColorAt(0,QColor(0,51,102,150));//亮天蓝色
    axisRectGradient.setColorAt(1,QColor("#1A1F32"));//深蓝色
    customPlot->axisRect()->setBackground(axisRectGradient);

    disconnect(customPlot,&QCustomPlot::beforeReplot,0,0);//断开beforeReplot连接


}

//legend与坐标曲线同步连接
void BluetoothWideget::selectionChanged(QCustomPlot* customPlot)
{
    if(!stepLed01->isStatus()){
      // 同步选择底轴，并将轴和刻度标签作为一个可选对象进行处理
      if (customPlot->xAxis->selectedParts().testFlag(QCPAxis::spAxis) || customPlot->xAxis->selectedParts().testFlag(QCPAxis::spTickLabels))
      {
        customPlot->xAxis->setSelectedParts(QCPAxis::spAxis|QCPAxis::spTickLabels);
      }
      // 同步选择左轴，并将轴和刻度标签作为一个可选对象进行处理
      if (customPlot->yAxis->selectedParts().testFlag(QCPAxis::spAxis) || customPlot->yAxis->selectedParts().testFlag(QCPAxis::spTickLabels))
      {
        customPlot->yAxis->setSelectedParts(QCPAxis::spAxis|QCPAxis::spTickLabels);
      }
      // 将图形的选择与相应图例项的选择同步
      for (int i=0; i<customPlot->graphCount(); ++i)
      {
        selectedGraph =customPlot->graph(i);
        QCPPlottableLegendItem *item = customPlot->legend->itemWithPlottable(selectedGraph);
        if (item->selected() || selectedGraph->selected())
        {
          item->setSelected(true);
          selectedGraph->setSelection(QCPDataSelection(selectedGraph->data()->dataRange()));

        }
      }
    }
}

void BluetoothWideget::mouseMove(QMouseEvent *e)
{   if(!stepLed01->isStatus()){
        x = e->pos().x();
    }

}


void BluetoothWideget::updateData()
{
//    qDebug()<<"frequency_right_pressure:"<<frequency_right_pressure<<"frequency_left_pressure:"<<frequency_left_pressure;
//    qDebug()<<"nine_pressure_data_right:"<<nine_pressure_data_right<<"nine_pressure_data_list_original_location_right:"<<nine_pressure_data_list_original_location_right.count()<<"nine_pressure_data_temp_right:"<<nine_pressure_data_temp_right.count();

    if(frequency_right_pressure.size()==9){

        for(int i = 0 ; i < 5 ; i++){
             showPlot(plotListR.at(i), frequency_right_pressure,i+1);
        }

    }else{
        QList<int> list;
        list<<0<<0<<0<<0<<0<<0<<0<<0<<0;
        for(int i = 0 ; i < 5 ; i++){
             showPlot(plotListR.at(i), list,i+1);
        }
    }
    if(frequency_left_pressure.size()==9){

        for(int i = 0 ; i < 5 ; i++){
             showPlot(plotListL.at(i), frequency_left_pressure,i+6);
        }

    }else{
        QList<int> list;
        list<<0<<0<<0<<0<<0<<0<<0<<0<<0;
        for(int i = 0 ; i < 5 ; i++){
             showPlot(plotListL.at(i), list,i+6);
        }
    }
    if(frequency_right_pressure.size()==9&&frequency_left_pressure.size()==9){
        QList<int> frequency_pressure;
        for(int i  = 0 ; i < 9 ; i++){
        frequency_pressure.append(frequency_right_pressure.at(i)+frequency_left_pressure.at(i));
        }
//        qDebug()<<"frequency_pressure:"<<frequency_pressure;
        for(int i = 0 ; i < 5 ; i++){
             showPlot(plotListAll.at(i),frequency_pressure,i+11);
        }
        frequency_pressure.clear();
    }else{
        QList<int> list;
        list<<0<<0<<0<<0<<0<<0<<0<<0<<0;
        for(int i = 0 ; i < 5 ; i++){
             showPlot(plotListAll.at(i), list,i+11);
        }
    }
    frequency_right_pressure.clear();
    frequency_left_pressure.clear();



////    热力图数值注入
///     左右脚

    if(nine_pressure_data_right.size()==9&&nine_pressure_data_list_original_location_right.size()==9&&nine_pressure_data_temp_right.size()==9){
        showHeatMap(nine_pressure_data_right,nine_pressure_data_list_original_location_right,nine_pressure_data_temp_right,0);
    }
        nine_pressure_data_right.clear();
        nine_pressure_data_list_original_location_right.clear();
        nine_pressure_data_temp_right.clear();

    if(nine_pressure_data_left.size()==9&&nine_pressure_data_list_original_location_left.size()==9&&nine_pressure_data_temp_left.size()==9){
        showHeatMap(nine_pressure_data_left,nine_pressure_data_list_original_location_left,nine_pressure_data_temp_left,1);
    }
        nine_pressure_data_left.clear();
        nine_pressure_data_list_original_location_left.clear();
        nine_pressure_data_temp_left.clear();
}


// 曲线更新绘图
void BluetoothWideget::showPlot(QCustomPlot *customPlot, QList<int> num,int value)
{
    // 给曲线添加数据
    switch(value) {
    case 1:
    right_foot->allPressure.graph.pGraph1_1->addData(timeX,num.at(0));  //x轴换成时间，y轴换成压力值数据
    right_foot->allPressure.graph.pGraph1_2->addData(timeX,num.at(1));
    right_foot->allPressure.graph.pGraph1_3->addData(timeX,num.at(2));
    right_foot->allPressure.graph.pGraph1_4->addData(timeX,num.at(3));
    right_foot->allPressure.graph.pGraph1_5->addData(timeX,num.at(4));
    right_foot->allPressure.graph.pGraph1_6->addData(timeX,num.at(5));
    right_foot->allPressure.graph.pGraph1_7->addData(timeX,num.at(6));
    right_foot->allPressure.graph.pGraph1_8->addData(timeX,num.at(7));
    right_foot->allPressure.graph.pGraph1_9->addData(timeX,num.at(8));
//    right_foot->allPressure.graph.pGraph1_1->removeDataBefore(timeX - 5);
//    right_foot->allPressure.graph.pGraph1_2->removeDataBefore(timeX - 5);
//    right_foot->allPressure.graph.pGraph1_3->removeDataBefore(timeX - 5);
//    right_foot->allPressure.graph.pGraph1_4->removeDataBefore(timeX - 5);
//    right_foot->allPressure.graph.pGraph1_5->removeDataBefore(timeX - 5);
//    right_foot->allPressure.graph.pGraph1_6->removeDataBefore(timeX - 5);
//    right_foot->allPressure.graph.pGraph1_7->removeDataBefore(timeX - 5);
//    right_foot->allPressure.graph.pGraph1_8->removeDataBefore(timeX - 5);
//    right_foot->allPressure.graph.pGraph1_9->removeDataBefore(timeX - 5);
    if(allPRangeR<*std::max_element(num.begin(),num.end())){
         allPRangeR = *std::max_element(num.begin(),num.end());
         customPlot->yAxis->setRange(allPRangeR+10,-5);        // 设置y坐标轴显示范围

    }

    break;
    case 2:

        FMBPdata1 = num.at(0)+num.at(1)+num.at(2)+num.at(3);
        FMBPdata2 = num.at(4)+num.at(5);
        FMBPdata3 = num.at(6)+num.at(7)+num.at(8);
        FMBPdata = (FMBPdata1>FMBPdata2?FMBPdata1:FMBPdata2)>FMBPdata3?(FMBPdata1>FMBPdata2?FMBPdata1:FMBPdata2):FMBPdata3;

    right_foot->frontMidBackPressure.graph.FMBpGraph1_1->addData(timeX, num.at(0)+num.at(1)+num.at(2)+num.at(3));
    right_foot->frontMidBackPressure.graph.FMBpGraph1_2->addData(timeX, num.at(4)+num.at(5));
    right_foot->frontMidBackPressure.graph.FMBpGraph1_3->addData(timeX, num.at(6)+num.at(7)+num.at(8));
//    right_foot->frontMidBackPressure.graph.FMBpGraph1_1->removeDataBefore(timeX - 5);
//    right_foot->frontMidBackPressure.graph.FMBpGraph1_2->removeDataBefore(timeX - 5);
//    right_foot->frontMidBackPressure.graph.FMBpGraph1_3->removeDataBefore(timeX - 5);
    if(FMBPRangeR<FMBPdata){
         FMBPRangeR = FMBPdata;
         customPlot->yAxis->setRange(FMBPRangeR+10,-5);        // 设置y坐标轴显示范围
    }
    break;
    case 3:
        FBPdata1 = num.at(0)+num.at(1)+num.at(2)+num.at(3);
        FBPdata2 = num.at(4)+num.at(5)+num.at(6)+num.at(7)+num.at(8);
        FBPdata = (FBPdata1>FBPdata2?FBPdata1:FBPdata2);
    right_foot->frontBackPressure.graph.FBGraph1_1->addData(timeX, num.at(0)+num.at(1)+num.at(2)+num.at(3));
    right_foot->frontBackPressure.graph.FBGraph1_2->addData(timeX, num.at(4)+num.at(5)+num.at(6)+num.at(7)+num.at(8));
//    right_foot->frontBackPressure.graph.FBGraph1_1->removeDataBefore(timeX - 5);
//    right_foot->frontBackPressure.graph.FBGraph1_2->removeDataBefore(timeX - 5);
    if(FBPRangeR<FBPdata){
         FBPRangeR = FBPdata;
         customPlot->yAxis->setRange(FBPRangeR+10,-5);        // 设置y坐标轴显示范围
    }
    break;
    case 4:
        IOPdata1 = num.at(3)+num.at(5)+num.at(6);
        IOPdata2 = num.at(0)+num.at(2)+num.at(4)+num.at(7);
        IOPdata = (IOPdata1>IOPdata2?IOPdata1:IOPdata2);
    right_foot->insideOutsidePressure.graph.IOGraph1_1->addData(timeX,num.at(0)+num.at(2)+num.at(4)+num.at(7));
    right_foot->insideOutsidePressure.graph.IOGraph1_2->addData(timeX,num.at(3)+num.at(5)+num.at(6));
//    right_foot->insideOutsidePressure.graph.IOGraph1_1->removeDataBefore(timeX - 5);
//    right_foot->insideOutsidePressure.graph.IOGraph1_2->removeDataBefore(timeX - 5);
    if(IOPRangeR<IOPdata){
         IOPRangeR = IOPdata;
         customPlot->yAxis->setRange(IOPRangeR+10,-5);        // 设置y坐标轴显示范围
    }
    break;
    case 5:
    right_foot->bodyPressure.graph.BGraph1_1->addData(timeX,num.at(0));
    right_foot->bodyPressure.graph.BGraph1_2->addData(timeX,num.at(8));
    break;
    case 6:
    left_foot->allPressure.graph.pGraph1_1->addData(timeX,num.at(0));    //x轴换成时间，y轴换成压力值数据
    left_foot->allPressure.graph.pGraph1_2->addData(timeX,num.at(1));
    left_foot->allPressure.graph.pGraph1_3->addData(timeX,num.at(2));
    left_foot->allPressure.graph.pGraph1_4->addData(timeX,num.at(3));
    left_foot->allPressure.graph.pGraph1_5->addData(timeX,num.at(4));
    left_foot->allPressure.graph.pGraph1_6->addData(timeX,num.at(5));
    left_foot->allPressure.graph.pGraph1_7->addData(timeX,num.at(6));
    left_foot->allPressure.graph.pGraph1_8->addData(timeX,num.at(7));
    left_foot->allPressure.graph.pGraph1_9->addData(timeX,num.at(8));
//    left_foot->allPressure.graph.pGraph1_1->removeDataBefore(timeX - 5);
//    left_foot->allPressure.graph.pGraph1_2->removeDataBefore(timeX - 5);
//    left_foot->allPressure.graph.pGraph1_3->removeDataBefore(timeX - 5);
//    left_foot->allPressure.graph.pGraph1_4->removeDataBefore(timeX - 5);
//    left_foot->allPressure.graph.pGraph1_5->removeDataBefore(timeX - 5);
//    left_foot->allPressure.graph.pGraph1_6->removeDataBefore(timeX - 5);
//    left_foot->allPressure.graph.pGraph1_7->removeDataBefore(timeX - 5);
//    left_foot->allPressure.graph.pGraph1_8->removeDataBefore(timeX - 5);
//    left_foot->allPressure.graph.pGraph1_9->removeDataBefore(timeX - 5);
    if(allPRangeL<*std::max_element(num.begin(),num.end())){
         allPRangeL = *std::max_element(num.begin(),num.end());
         customPlot->yAxis->setRange(allPRangeL+10,-5);        // 设置y坐标轴显示范围
    }
    break;
    case 7:
        FMBPdata1 = num.at(0)+num.at(1)+num.at(2)+num.at(3);
        FMBPdata2 = num.at(4)+num.at(5);
        FMBPdata3 = num.at(6)+num.at(7)+num.at(8);
        FMBPdata = (FMBPdata1>FMBPdata2?FMBPdata1:FMBPdata2)>FMBPdata3?(FMBPdata1>FMBPdata2?FMBPdata1:FMBPdata2):FMBPdata3;

    left_foot->frontMidBackPressure.graph.FMBpGraph1_1->addData(timeX,num.at(0)+num.at(1)+num.at(2)+num.at(3));
    left_foot->frontMidBackPressure.graph.FMBpGraph1_2->addData(timeX,num.at(4)+num.at(5));
    left_foot->frontMidBackPressure.graph.FMBpGraph1_3->addData(timeX,num.at(6)+num.at(7)+num.at(8));
//    left_foot->frontMidBackPressure.graph.FMBpGraph1_1->removeDataBefore(timeX - 5);
//    left_foot->frontMidBackPressure.graph.FMBpGraph1_2->removeDataBefore(timeX - 5);
//    left_foot->frontMidBackPressure.graph.FMBpGraph1_3->removeDataBefore(timeX - 5);
    if(FMBPRangeL<FMBPdata){
         FMBPRangeL = FMBPdata;
         customPlot->yAxis->setRange(FMBPRangeL+10,-5);        // 设置y坐标轴显示范围
    }
    break;
    case 8:
        FBPdata1 = num.at(0)+num.at(1)+num.at(2)+num.at(3);
        FBPdata2 = num.at(4)+num.at(5)+num.at(6)+num.at(7)+num.at(8);
        FBPdata = (FBPdata1>FBPdata2?FBPdata1:FBPdata2);
    left_foot->frontBackPressure.graph.FBGraph1_1->addData(timeX,num.at(0)+num.at(1)+num.at(2)+num.at(3));
    left_foot->frontBackPressure.graph.FBGraph1_2->addData(timeX,num.at(4)+num.at(5)+num.at(6)+num.at(7)+num.at(8));
//    left_foot->frontBackPressure.graph.FBGraph1_1->removeDataBefore(timeX - 5);
//    left_foot->frontBackPressure.graph.FBGraph1_2->removeDataBefore(timeX - 5);
    if(FBPRangeL<FBPdata){
         FBPRangeL = FBPdata;
         customPlot->yAxis->setRange(FBPRangeL+10,-5);        // 设置y坐标轴显示范围
    }
    break;
    case 9:
        IOPdata1 = num.at(3)+num.at(5)+num.at(6);
        IOPdata2 = num.at(0)+num.at(2)+num.at(4)+num.at(7);
        IOPdata = (IOPdata1>IOPdata2?IOPdata1:IOPdata2);
    left_foot->insideOutsidePressure.graph.IOGraph1_1->addData(timeX,num.at(0)+num.at(2)+num.at(4)+num.at(7));
    left_foot->insideOutsidePressure.graph.IOGraph1_2->addData(timeX,num.at(3)+num.at(5)+num.at(6));
//    left_foot->insideOutsidePressure.graph.IOGraph1_1->removeDataBefore(timeX - 5);
//    left_foot->insideOutsidePressure.graph.IOGraph1_2->removeDataBefore(timeX - 5);
    if(IOPRangeL<IOPdata){
         IOPRangeL = IOPdata;
         customPlot->yAxis->setRange(IOPRangeL+10,-5);        // 设置y坐标轴显示范围
    }
    break;
    case 10:
     left_foot->bodyPressure.graph.BGraph1_1->addData(timeX,num.at(0));
     left_foot->bodyPressure.graph.BGraph1_2->addData(timeX,num.at(8));
    break;
    case 11:
        double_foot->allPressure.graph.pGraph1_1->addData(timeX, num.at(0));  //x轴换成时间，y轴换成压力值数据
        double_foot->allPressure.graph.pGraph1_2->addData(timeX, num.at(1));
        double_foot->allPressure.graph.pGraph1_3->addData(timeX, num.at(2));
        double_foot->allPressure.graph.pGraph1_4->addData(timeX, num.at(3));
        double_foot->allPressure.graph.pGraph1_5->addData(timeX, num.at(4));
        double_foot->allPressure.graph.pGraph1_6->addData(timeX, num.at(5));
        double_foot->allPressure.graph.pGraph1_7->addData(timeX, num.at(6));
        double_foot->allPressure.graph.pGraph1_8->addData(timeX, num.at(7));
        double_foot->allPressure.graph.pGraph1_9->addData(timeX, num.at(8));

//        double_foot->allPressure.graph.pGraph1_1->removeDataBefore(timeX - 5);  //x轴换成时间，y轴换成压力值数据
//        double_foot->allPressure.graph.pGraph1_2->removeDataBefore(timeX - 5);
//        double_foot->allPressure.graph.pGraph1_3->removeDataBefore(timeX - 5);
//        double_foot->allPressure.graph.pGraph1_4->removeDataBefore(timeX - 5);
//        double_foot->allPressure.graph.pGraph1_5->removeDataBefore(timeX - 5);
//        double_foot->allPressure.graph.pGraph1_6->removeDataBefore(timeX - 5);
//        double_foot->allPressure.graph.pGraph1_7->removeDataBefore(timeX - 5);
//        double_foot->allPressure.graph.pGraph1_8->removeDataBefore(timeX - 5);
//        double_foot->allPressure.graph.pGraph1_9->removeDataBefore(timeX - 5);
        if(allPRange<*std::max_element(num.begin(),num.end())){
             allPRange = *std::max_element(num.begin(),num.end());
             customPlot->yAxis->setRange(allPRange+10,-5);        // 设置y坐标轴显示范围
        }

    break;
    case 12:
        FMBPdata1 = num.at(0)+num.at(1)+num.at(2)+num.at(3);
        FMBPdata2 = num.at(4)+num.at(5);
        FMBPdata3 = num.at(6)+num.at(7)+num.at(8);
        FMBPdata = (FMBPdata1>FMBPdata2?FMBPdata1:FMBPdata2)>FMBPdata3?(FMBPdata1>FMBPdata2?FMBPdata1:FMBPdata2):FMBPdata3;

        double_foot->frontMidBackPressure.graph.FMBpGraph1_1->addData(timeX, num.at(0)+num.at(1)+num.at(2)+num.at(3));
        double_foot->frontMidBackPressure.graph.FMBpGraph1_2->addData(timeX, num.at(4)+num.at(5));
        double_foot->frontMidBackPressure.graph.FMBpGraph1_3->addData(timeX, num.at(6)+num.at(7)+num.at(8));
//        double_foot->frontMidBackPressure.graph.FMBpGraph1_1->removeDataBefore(timeX - 5);
//        double_foot->frontMidBackPressure.graph.FMBpGraph1_2->removeDataBefore(timeX - 5);
//        double_foot->frontMidBackPressure.graph.FMBpGraph1_3->removeDataBefore(timeX - 5);
        if(FMBPRange<FMBPdata){
             FMBPRange = FMBPdata;
             customPlot->yAxis->setRange(FMBPRange+10,-5);        // 设置y坐标轴显示范围
        }

    break;
    case 13:
        FBPdata1 = num.at(0)+num.at(1)+num.at(2)+num.at(3);
        FBPdata2 = num.at(4)+num.at(5)+num.at(6)+num.at(7)+num.at(8);
        FBPdata = (FBPdata1>FBPdata2?FBPdata1:FBPdata2);
        double_foot->frontBackPressure.graph.FBGraph1_1->addData(timeX, num.at(0)+num.at(1)+num.at(2)+num.at(3));
        double_foot->frontBackPressure.graph.FBGraph1_2->addData(timeX, num.at(4)+num.at(5)+num.at(6)+num.at(7)+num.at(8));
//        double_foot->frontBackPressure.graph.FBGraph1_1->removeDataBefore(timeX - 5);
//        double_foot->frontBackPressure.graph.FBGraph1_2->removeDataBefore(timeX - 5);
        if(FBPRange<FBPdata){
             FBPRange = FBPdata;
             customPlot->yAxis->setRange(FBPRange+10,-5);        // 设置y坐标轴显示范围
        }

    break;
    case 14:
        IOPdata1 = num.at(3)+num.at(5)+num.at(6);
        IOPdata2 = num.at(0)+num.at(2)+num.at(4)+num.at(7);
        IOPdata = (IOPdata1>IOPdata2?IOPdata1:IOPdata2);

        double_foot->insideOutsidePressure.graph.IOGraph1_1->addData(timeX,num.at(0)+num.at(2)+num.at(4)+num.at(7));
        double_foot->insideOutsidePressure.graph.IOGraph1_2->addData(timeX,num.at(3)+num.at(5)+num.at(6));
//        double_foot->insideOutsidePressure.graph.IOGraph1_1->removeDataBefore(timeX - 5);
//        double_foot->insideOutsidePressure.graph.IOGraph1_2->removeDataBefore(timeX - 5);
        if(IOPRange<IOPdata){
             IOPRange = IOPdata;
             customPlot->yAxis->setRange(IOPRange+10,-5);        // 设置y坐标轴显示范围
        }
    break;
    case 15:
          double_foot->bodyPressure.graph.BGraph1_1->addData(timeX,num.at(0));
          double_foot->bodyPressure.graph.BGraph1_2->addData(timeX,num.at(8));
    break;
    }
    customPlot->xAxis->setRange(0,timeX );


//    if(timeX > 5){
//        customPlot->xAxis->setRange(timeX-5,timeX );
//     }else{
//        customPlot->xAxis->setRange(0,timeX );
//    }
    customPlot->replot(QCustomPlot::rpQueuedReplot);

    num.clear();
}




//把病人文件加入菜单列表
void BluetoothWideget::searchUserInfo(QJsonObject object)
{
    QDir dir;
    QString path = QDir::currentPath();
    qDebug()<<"当前路径："<<path;
    QString username = object.value("姓名").toString();  //病人文件夹名
    dir.cd(path);  //进入文件夹
    QStringList list1 = dir.entryList();

    dir.cd(path+"/"+username);   //进入病人文件夹
     QStringList list2 = dir.entryList();
     qDebug() << path+"/"+username;

     //筛选文件夹类型
    QStringList filenames;
    filenames << "*.json";
    QStringList files = dir.entryList(filenames,QDir::Files|QDir::Readable,QDir::Name);  //获取目录里所有指定文件
    qDebug()<<"json文件："<<files;

     QList<QTreeWidgetItem *> items;

    //遍历文件夹下所有文件夹
     //添加第一层菜单 病人名字
     QTreeWidgetItem *usernameFile = new QTreeWidgetItem(ui->treeWidget);
     usernameFile->setText(0, username);
     for(int i=0;i<files.length();i++) {
         //添加第二层菜单-病人信息文件字
         QTreeWidgetItem *userinfoFile = new QTreeWidgetItem(usernameFile);
         userinfoFile->setText(0,files[i]);
         usernameFile->addChild(userinfoFile);
      }


}


//右键菜单
void BluetoothWideget::SlotCustomContextMenuRequested(const QPoint &)
{
   QAction *m_pact = new QAction(QStringLiteral("删除"), this);
    //QAction *pDelAction_1 = new QAction(QStringLiteral("重命名"), this);
    QMenu *popMenu = new QMenu(this);//定义右键弹出菜单
    popMenu->addAction(m_pact);//往菜单内添加QAction
    // QAction *deleAction = cmenu->addAction("Delete");//另一种创建方式
    connect(m_pact, SIGNAL(triggered()), this, SLOT(deleteItemFunction()));
    popMenu->exec(QCursor::pos());//弹出右键菜单，菜单位置为光标位置
}

void BluetoothWideget::deleteItemFunction()
{
    QTreeWidgetItem * currentItem = ui->treeWidget->currentItem();//获取当前节点
//    qDebug()<<"当前节点："<<ui->treeWidget->currentItem()->text(0);
        if (currentItem == NULL)
        {
            return;
        }
        else  {
            delete currentItem;

//            //同时从文件夹里删除
//            QString username = ui->treeWidget->currentItem()->text(0);

        }
}



void BluetoothWideget::on_closeButton_clicked()
{
    QPropertyAnimation *animation = new QPropertyAnimation(this, "windowOpacity");
     animation->setDuration(500);
     animation->setStartValue(1);
     animation->setEndValue(0);
     animation->start();
     connect(animation, SIGNAL(finished()), this, SLOT(close()));
     delete ui;
}
